/*
 * MembraneTarget.java
 *
 * Created on Dec 20, 2011
 *
 * Copyright (c) 2011 Artur Rataj.
 *
 * This code is distributed under the terms of the GNU Library
 * General Public License, either version 3 of the license or, at
 * your option, any later version.
 */

package pl.gliwice.iitis.spatialnn.network;

import java.text.DecimalFormat;
import pl.gliwice.iitis.graphutils.Point;
import pl.gliwice.iitis.spatialnn.network.construct.InputType;

/**
 * A target being a membrane's point.
 * 
 * @author Artur Rataj
 */
public class MembraneTarget extends AbstractTarget {
    /**
     * Key of the target membrane.
     */
    public String key;
    /**
     * Expression of a point on the target's membrane.
     */
    public double expression;
    
    /**
     * Formats what <code>toString</code>'s <code>membrane</code> produces.
     */
    final protected DecimalFormat formatter = new DecimalFormat("#0.##");
    
    /**
     * Creates a new instance of <code>NeuronTarget</code>.
     * 
     * @param key key of the target membrane
     * @param expression expression of a point on the target's membrane
     */
    public MembraneTarget(String key, double expression) {
        super();
        this.key = key;
        this.expression = expression;
    }
    @Override
    public Point getLoc(Network network) {
        Membrane m = network.membraneMap.get(key);
        Point beg = m.locBeg;
        Point end = m.locEnd;
        Point out = new Point(
                beg.x + (expression - m.expressionBeg)*(end.x - beg.x)/
                    (m.expressionEnd - m.expressionBeg),
                beg.y + (expression - m.expressionBeg)*(end.y - beg.y)/
                    (m.expressionEnd - m.expressionBeg));
        return out;
    }
    @Override
    public boolean componentsEqual(AbstractTarget other) {
        if(other instanceof MembraneTarget) {
            MembraneTarget m = (MembraneTarget)other;
            return key.equals(m.key);
        } else
            return false;
    }
    @Override
    public String toStringExact() {
        return "\"" + key + "\":" + expression;
    }
    @Override
    public String toString() {
        return "\"" + key + "\":" + formatter.format(expression);
    }
}
